/***************************************************************************
 创建者: 华磊
 开始时间: 2019.11.23
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef JOYSTICKDELEGATE_H
#define JOYSTICKDELEGATE_H
#include "gSoapFoundation/soapStub.h"
#include <QThread>

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <linux/joystick.h>
#include "listop.h"
#if 1
typedef struct _axes_t {
    int x;
    int y;
} AXES_T;

#endif

extern "C"


class JoyStickView;

//enum E_JOG_KEY
//{
//    E_JOG_KEY_NONE=0,
//    E_JOG_KEY_1P=1,//r+
//    E_JOG_KEY_1N=2,//r-
//    E_JOG_KEY_2P=3,//x+
//    E_JOG_KEY_2N=4,//x-
//    E_JOG_KEY_3P=5,
//    E_JOG_KEY_3N=6,
//    E_JOG_KEY_4P=7,
//    E_JOG_KEY_4N=8,
//    E_JOG_KEY_5P=9,
//    E_JOG_KEY_5N=10,
//    E_JOG_KEY_6P=11,
//    E_JOG_KEY_6N=12,
//};
//１２个轴动
//2个速度设置
//1个坐标系切换
//1个启动、停止
//１个复位和使能。
//１个界面设置默认新增的指令类型，直线、圆弧、关节。
//1个切换工作模式

struct JoyStickAxisInfo
{
    double x;
    double y;
};

class JoyStickDelegate: public QThread
{
    Q_OBJECT
public:
    JoyStickDelegate(QString configFileIn);

    int initialJoyStickDevice();

signals:
    void showView_signal(bool isShow);
    void lInstruction();
    void cease_signal();
    void setAxesValues_signal(QString contStr,QString index_axes,QString coordinates_x,QString coordinates_y);
    void setButtonState_signal(QString contStr,QString index,QString state);

private:

    void displayJoyStickView_slot(bool isShow);
    void initConnect();
    int playJoystickMusic(QString musicNameIn, int musicPlayTimes, int musicVolume);
    void stopMusic();
    int conversionVolume(int volumeIn);
    int initialConfigFile(QString configFileName);
    virtual void run();
    void joyStickMonitor_agv();
    void updateJoyStickKeyValue();
    void setButtonState_agv(QString contStr,QString index,QString state);
    void setAxesValues(QString contStr,QString index_axes,QString coordinates_x,QString coordinates_y);
    void stopJogMove();
    bool isJoyStickJogNeedStop();
    bool isJoyStickJogNeedStart();
    void updateJogValueFromAxis(double &xOut,double &yOut,double &rOut);
    void addMsg(int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0 , QString strIn="");

private slots:
    void setManualSpeedPlusSlot();
    void setManualSpeedReduceSlot();
    void setJointCoordinateSlot();
    void runSlot();
    int programAllCease();
    int setAllRunModel();

private:

    JoyStickView *joyStickView;

    bool isInJogMoving;//是否为手动运行状态

//    E_JOG_KEY pressedKey;
    UserInfo userInfo;

    int fd, rc;
    int op_times;
    char number_of_axes;
    char number_of_btns;
    char js_name_str[128];
    unsigned int buttons_state;
    AXES_T* tp_axes ;
    int i, print_init_stat;
    struct js_event jse;

    double moveRatio_x;
    double moveRatio_y;
    double moveRatio_r;


    JoyStickAxisInfo axis2Info;//X 和　Ｒ ,固定速度
    JoyStickAxisInfo axis0Info;//X 和　Ｒ，可调速度
    JoyStickAxisInfo axis1Info;//X 和　Y，可调速度

    bool isLeftYOn;
    bool isRightYOn;

    double manualSpeedRatio;

    bool isUseJoyStick;
    bool isForAgvControl;
    bool isPlayNoticeSound;
    QString joystickDevName;

};

#endif // JOYSTICKDELEGATE_H
